package com.java.zhuiyun.api.back_course.service.impl;

import com.java.zhuiyun.api.back_course.entity.BackCourse;
import com.java.zhuiyun.api.back_course.mapper.BackCourseMapper;
import com.java.zhuiyun.api.back_course.model.BackCourseModel;
import com.java.zhuiyun.api.back_course.service.BackCourseService;
import com.java.zhuiyun.api.course_unit.entity.CourseUnit;
import com.java.zhuiyun.api.dict_navigation_tabs.entity.SysDictPc;
import com.java.zhuiyun.api.sys_dict.entity.SysDict;
import com.java.zhuiyun.api.teacher.entity.Teacher;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.UploadFileUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName BackCourseServiceImpl
 * @Description TODO
 * @Author 杨友朋
 * @Date 2023/8/14 15:15
 **/
@Service
public class BackCourseServiceImpl implements BackCourseService {
    @Resource
    BackCourseMapper backCourseMapper;

    /**
     * @Author 杨友朋
     * @Description TODO 查询课程
     * @Date 2023/8/15 21:00
     * @Return
     * @param
     */
    @Override
    public PageResult selectCourse(BackCourseModel backCourse, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<BackCourseModel> backCourseModels = backCourseMapper.selectCourse(backCourse);
        PageResult pageResult = MybatisPageHelper.getPageResult(backCourseModels);
        return pageResult;
    }

    @Override
    public PageResult teacherSelectCourse(BackCourseModel backCourse, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<BackCourseModel> backCourseModels = backCourseMapper.teacherSelectCourse(backCourse);
        PageResult pageResult = MybatisPageHelper.getPageResult(backCourseModels);
        return pageResult;
    }

    @Override
    public Teacher selectTeacherId(Integer userId) {
        Teacher teacher = backCourseMapper.selectTeacherId(userId);
        return teacher;
    }

    /**
     * @Author 杨友朋
     * @Description TODO 添加课程
     * @Date 2023/8/15 21:00
     * @Return
     * @param
     */
    @Transactional
    @Override
    public CommonResult addCourse(MultipartFile file, BackCourse backCourse) {
        //上传图片至服务器
        String s = UploadFileUtils.uploadFile(file);
        //判断是否成功
        if(s!=""){
            backCourse.setCourseImg(s);
        }
        //创建时间
        backCourse.setCreateTime(new Date());
        //修改时间
        backCourse.setUpdateTime(new Date());
        // 截取字符串
        String subjectId = backCourse.getSubjectId();
        int lastCommaIndex = subjectId.lastIndexOf(",");
        if (lastCommaIndex != -1) {
            subjectId = subjectId.substring(lastCommaIndex + 1);
        }
        backCourse.setSubjectId(subjectId);
        backCourse.setUpTime(new Date());
        int i = backCourseMapper.insertSelective(backCourse);
        if(i>0){
            return CommonResult.ok();
        }
        return CommonResult.error(500,"添加失败");
    }

    /**
     * @Author 杨友朋
     * @Description TODO 根据id单查课程
     * @Date 2023/8/15 21:01
     * @Return
     * @param
     */
    @Override
    public CommonResult selectCourseById(Integer id) {
        return CommonResult.ok(backCourseMapper.selectCourseById(id));
    }

    /**
    * @Author 杨友朋
    * @Description TODO 删除课程
    * @Date 2023/8/17 19:38
    * @Return
    * @param
    */
    @Override
    public CommonResult delCourse(Integer id) {
        Integer integer = backCourseMapper.delCourse(id);
        if(integer>0){
            return CommonResult.ok();
        }
        return CommonResult.error(500,"删除课程失败");
    }

    /*
     * @Author WX
     * @Description 查询课程数据
     * @Date 2023/8/28 8:44
     * @Return
     * @param null
     */
    @Override
    public List<SysDictPc> selectDict() {
        String d="subject";
        List<SysDictPc> dict=backCourseMapper.selectDict(d);
        return dict;
    }

    @Override
    public CommonResult updateCourse(MultipartFile file1, BackCourse backCourse) {
        if(file1!=null){
            //上传图片至服务器
            String s = UploadFileUtils.uploadFile(file1);
            //判断是否成功
            if(s==""){
                return CommonResult.error(500,"添加失败");
            }
            backCourse.setCourseImg(s);
        }
        //修改时间
        backCourse.setUpdateTime(new Date());
        // 截取字符串
        String subjectId = backCourse.getSubjectId();
        int lastCommaIndex = subjectId.lastIndexOf(",");
        if (lastCommaIndex != -1) {
            subjectId = subjectId.substring(lastCommaIndex + 1);
        }
        backCourse.setSubjectId(subjectId);
        int i = backCourseMapper.updateCourse(backCourse);
        if(i>0){
            return CommonResult.ok();
        }
        return CommonResult.error(500,"添加失败");
    }

    /**
     * @Author 杨友朋
     * @Description TODO 更改状态
     * @Date 2023/8/24 9:23
     * @Return
     * @param
     */
    @Override
    public CommonResult upSwitch(BackCourse backCourse) {
        backCourse.setUpdateTime(new Date());
        backCourse.setUpTime(new Date());
        backCourseMapper.updateSwitch(backCourse);
        return CommonResult.ok();
    }
}
